<?php
class Admin_news extends MY_Controller {
	public function __construct()
	{
		$this->data["menu"] = "news";
		parent::__construct();		
		$this->data["base_link"] = base_index_admin . "news/";
	}
	
	public function index()
	{	
		$pgintf = "";
		$listbody = "";
		$total_num = $this->newsModel->count_all();
		if($total_num > 0){
			list($pgintf, $startnum) = createPaging($_SERVER["PHP_SELF"], strip_tags(@$_GET["page"]), $total_num, $this->data["paging_value"], $_SERVER["QUERY_STRING"]);
			$i = 1;
			$result = $this->newsModel->get_all($this->data["paging_value"], $startnum, "news.news_id DESC", array("lan" => $this->data["language"]));			
			foreach($result as $news){
				$visible = $news->visible;
				$visibleview = array_single_select($this->data["arr_visible"], $visible);
				$visibleclass = "";
				if($visible == "y"){
					$visibleclass = "class=\"on\"";
				}elseif($visible == "n"){
					$visibleclass = "class=\"off\"";
				}
								
				$listbody .= "<tr>\n";
				$listbody .= "<td>{$news->news_id}</td>\n";		
				$listbody .= "<td>{$news->newsTitle}</td>\n";
				$listbody .= "<td>{$news->createdDate}</td>\n";
				$listbody .= "<td {$visibleclass}>{$visibleview}</td>\n";
				$listbody .= "<td><a href=\"".$this->data["base_link"]."view/{$news->news_id}\" target=\"_blank\"><img src='".base_common_admin."images/ico_view.gif' /></a></td>\n";
				$listbody .= "<td><a href=\"".$this->data["base_link"]."edit/{$news->news_id}\"><img src='".base_common_admin."images/ico_edit.gif' /></a></td>\n";
				$listbody .= "<td><a href=\"javascript:void(0);\" onclick=\"delete_confirm({$news->news_id});\"><img src='".base_common_admin."images/ico_drop.gif' /></a></td>\n";
				$listbody .= "</tr>\n";
			}
		}else{
			$listbody .= "<tr>\n";
			$listbody .= "<td colspan='7'>".$this->data["lang"]["general"]["noresult"]."</td>\n";
			$listbody .= "</tr>\n";
		}
		
		$this->data["pgintf"] = $pgintf;
		$this->data["listbody"] = $listbody;
		@$this->data["msg"] = $_SESSION["msg"];
		unset($_SESSION["msg"]);
		
		$this->load->view('admin/news/news_list', $this->data);
	}
	
	public function view()
	{		
		$news_id = $this->uri->segment(4);
		$arr = array("news_id" => $news_id, "lan" => $this->data["language"]);		
		$query = $this->newsModel->get_view($arr);
		if($query->num_rows < 1)
			header("Location: " . $this->data["base_link"]);
		$this->data["fullDesc"] = $query->row()->fullDesc;
		$this->load->view('admin/news/news_view', $this->data);
	}
	
	public function add()
	{	
		@$createdDate = $_SESSION["createdDate"];
		@$image = $_SESSION["image"];
		@$visible 	= $_SESSION["visible"];		
		if(!isset($_SESSION["createdDate"]))
			$createdDate = date("Y/m/d");
			
		$imageView = "";
		if(!empty($image)){
			$file = $this->data["news_dir"] . $image;
			if(file_exists($file)){
				$imageView = "<div id='divImage'>".view_image($this->data["news_path"], $image, $this->data["news_dir"], $this->data["image_width"])."<br /><a href='javascript:void(0);' onclick='return message(".json_encode($file).", \"divImage\")'>".$this->data["lang"]["general"]["delete_image"]."</a><br /></div>";
			}
		}
		
		$arr_err = array();
		if(!empty($_SESSION["err_lang"])) {
			foreach($this->data["arr_lang"] as $la => $val){
				$lang_key = lang_key($la, "news_url");
				if(in_array($lang_key, $_SESSION["err_lang"])){
					$arr_err[$lang_key] = "<br /><span class=\"required\">" . $this->data["lang"]["news"]["errormsg_news_url"] . "</span>";
				}
			}
		}
		
		$err_chk = "";
		$errormsg_createdDate = "";
		if(!empty($_SESSION["err"])){
			$err = $_SESSION["err"];		
			if(preg_match("/01/", $err)){
				$errormsg_createdDate = "<br /><br /><span class=\"required\">".$this->data["lang"]["news"]["errormsg_createdDate"]."</span>";
				$err_chk++;
			}			
		}
		unset($_SESSION["err"]);
		$this->data["errormsg_createdDate"] = $errormsg_createdDate;
		
		$visible = !empty($visible) ? $visible : "y";
		$visibleradio = @array_radio($this->data["arr_visible"], $visible, "visible");
		
		unset($_SESSION["news_id"]);
		unset($_SESSION["createdDate"]);
		unset($_SESSION["image"]);
		unset($_SESSION["visible"]);
		unset($_SESSION["err"]);
		
		$lang_fields = lang_createFields($this->data["arr_lang"], $this->data["arr_fields_news"], $this->data["arr_lang_chk_empty"], $this->data["lang"]["general"]["err_empty"], $arr_err);
		
		$this->data["visibleradio"] = $visibleradio;
		$this->data["createdDate"] = $createdDate;
		$this->data["image"] = $image;
		$this->data["imageView"] = $imageView;
		$this->data["lang_fields"] = $lang_fields;
		
		$this->load->view('admin/news/news_add', $this->data);		
	}
	
	public function edit()
	{
		if(empty($_SESSION["news_id"])){
			$news_id = $this->uri->segment(4);
			$query = $this->newsModel->get_one($news_id);
			if($query->num_rows < 1)
				header("Location: " . $this->data["base_link"] . "add");
			$news = $query->row_array();
			$query->free_result();
			extract($news);
			$table = "newsLan";
			$where = "`news_id` = '$news_id'";	
			lang_addData($table, $where, $this->data["arr_fields_news"], $this->data["arr_lang"]);
		}
		else{
			extract($_SESSION);
		}
		
		$imageView = "";
		if(!empty($image)){
			$file = $this->data["news_dir"] . $image;
			if(file_exists($file)){
				$imageView = "<div id='divImage'>".view_image($this->data["news_path"], $image, $this->data["news_dir"], $this->data["image_width"])."<br /><a href='javascript:void(0);' onclick='return message(".json_encode($file).", \"divImage\", \"news\", \"news_id\", \"{$news_id}\", \"image\")'>".$this->data["lang"]["general"]["delete_image"]."</a><br /></div>";
			}
		}
		
		$arr_err = array();
		if(!empty($_SESSION["err_lang"])) {
			foreach($this->data["arr_lang"] as $la => $val){
				$lang_key = lang_key($la, "news_url");
				if(in_array($lang_key, $_SESSION["err_lang"])){
					$arr_err[$lang_key] = "<br /><span class=\"required\">" . $this->data["lang"]["news"]["errormsg_news_url"] . "</span>";
				}
			}
		}
		
		$err_chk = "";
		$errormsg_createdDate = "";
		if(!empty($_SESSION["err"])){
			$err = $_SESSION["err"];		
			if(preg_match("/01/", $err)){
				$errormsg_createdDate = "<br /><br /><span class=\"required\">".$this->data["lang"]["news"]["errormsg_createdDate"]."</span>";
				$err_chk++;
			}			
		}
		unset($_SESSION["err"]);
		$this->data["errormsg_createdDate"] = $errormsg_createdDate;
		
		$visible = !empty($visible) ? $visible : "y";
		$visibleradio = @array_radio($this->data["arr_visible"], $visible, "visible");
					
		$lang_fields = lang_createFields($this->data["arr_lang"], $this->data["arr_fields_news"], $this->data["arr_lang_chk_empty"], $this->data["lang"]["general"]["err_empty"], $arr_err);
		
		$this->data["news_id"] = $news_id;
		$this->data["visibleradio"] = $visibleradio;
		$this->data["createdDate"] = $createdDate;
		$this->data["image"] = $image;
		$this->data["imageView"] = $imageView;
		$this->data["lang_fields"] = $lang_fields;
		@$this->data["msg"] = $_SESSION["msg"];

		unset($_SESSION["news_id"]);
		unset($_SESSION["createdDate"]);
		unset($_SESSION["image"]);		
		unset($_SESSION["visible"]);
		unset($_SESSION["err"]);
		unset($_SESSION["msg"]);
		
		$this->load->view('admin/news/news_edit', $this->data);		
	}
	
	public function done()
	{
		if(empty($_POST)){
			header("Location: " . $this->data["base_link"] . "add/");
			exit();
		}
			
		POST_To_SESSION();	
		
		@$image = !empty($_FILES["image"]["name"]) ? $_FILES["image"]["name"] : $_POST["check_image"];
		if(!empty($_FILES["image"]["name"])){
			$date = date("YmdHis", time());
			upload_image("image", $this->data["news_dir"], $date."_1");
			@$image = $_SESSION["image"];
		}
		$_SESSION["image"] = $image;
			
		lang_checkErrEmpty($this->data["arr_lang"], $this->data["arr_fields_news"], $this->data["arr_lang_chk_empty"]);

		extract($_POST);
		
		$err = "";
		if(empty($createdDate)){
			$err .= "+01";
		}
				
		$arr = array();
		$linkret = $this->data["base_link"] . "add/";
		if(!empty($news_id)){
			$arr["news_id !="] = $news_id;
			$linkret = $this->data["base_link"] . "edit/" . $news_id;
		}

		$err_lang = array();
		foreach($this->data["arr_lang"] as $la => $val){
			if(!empty($_POST["news_url_" . $la])){
				$arr["lan"] = $la;
				$arr["news_url"] = $_POST["news_url_" . $la];
				if($this->newsModel->check_url($arr)->num_rows > 0){
					$err_lang[] = lang_key($la, "news_url");
				}					
			}
			else{
				$url = stripUnicode($_SESSION["newsTitle_" . $la]);							
				if(!empty($url)){
					$arr["lan"] = $la;
					$arr["news_url"] = $url;
					if($this->newsModel->check_url($arr)->num_rows > 0){
						unset($arr["news_url"]);
						$url = $url . "-";
						$str = "";						
						if(!empty($news_id)){							
							$str = $this->newsModel->check_old_url($url, array("lan" => $la, "news_id" => $news_id));							
						}
						if(empty($str)){
							$query = $this->newsModel->get_max_url($url, $arr);
							$num = 1;
							if($query->num_rows > 0){
								$row = $query->row();
								$num = end(explode("-", $row->news_url));
								$num++;
							}							
							$url .= $num;							
						}
						else
							$url = $str;
					}
					$_SESSION["news_url_" . $la] = $url;
				}
			}
		}		
		$_SESSION["err"] = $err;		
		$_SESSION["err_lang"] = array_merge($_SESSION["err_lang"], $err_lang);		
		if(!empty($_SESSION["err_lang"]) || !empty($err)){
			header("Location: " . $linkret);
			exit();
		}
		
		$data = array();
		$data["createdDate"] = $createdDate;
		$data["visible"] = $visible;		
		if(empty($news_id)){
			$news_id = $this->newsModel->insert_entry($data);
			$data = array();
			$data["image"] = "";
			if(!empty($image)){
				@copy($this->data["news_dir"] . $image, $this->data["news_dir"] . $news_id . ".jpg");
				@unlink($this->data["news_dir"] . $image);
				$data["image"] = $news_id . ".jpg";
				crop($this->data["news_dir"] . $news_id . ".jpg", $this->data["news_dir"] . $news_id . "_small.jpg", $this->data["image_width_list"], 0);
			}
			$this->newsModel->update_entry($news_id, $data);		
			$_SESSION["msg"] = "<p class=\"notice\">" . $this->data["lang"]["general"]["regist_msg"] . "</p>";
		}
		else{
			if(!empty($image) && $image != $news_id . ".jpg"){
				@unlink($this->data["news_dir"] . $news_id . ".jpg");
				@rename($this->data["news_dir"] . $image, $this->data["news_dir"] . $news_id . ".jpg");
				$data["image"] = $news_id . ".jpg";
				crop($this->data["news_dir"] . $news_id . ".jpg", $this->data["news_dir"] . $news_id . "_small.jpg", $this->data["image_width_list"], 0);				
			}			
			$this->newsModel->update_entry($news_id, $data);		
			$_SESSION["msg"] = "<p class=\"notice\">" . $this->data["lang"]["general"]["upload_done"] . "</p>";
		}
		
		$def = array("news_id" => $news_id);	
		$table = "newsLan";
		$colID = "newsLan_id";
		lang_accesstData($table ,$this->data["arr_lang"], $this->data["arr_fields_news"], $def, $this->data["arr_lang_chk_empty"], $colID);
			
		//unset
		unsetPOSTSESSION();
		unset($_SESSION["err_lang"]);
		unset($_SESSION["err"]);		
		
		header("Location: " . $this->data["base_link"] . "edit/" . $news_id);
	}
	
	function delete(){
		@$id = $_POST["id"];
		@$page = $_POST["page"];		
		if(!empty($id)){
			$this->newsModel->delete_entry($id);
			$pageView = "";
			if(!empty($page))
				$pageView = "?page=" . $page;
			$_SESSION["msg"] = "<p class=\"notice\">" . $this->data["lang"]["general"]["delete_msg"] . "</p>";
			header("Location: " . $this->data["base_link"] . $pageView);
		}
	}
}